package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.List;

@RestController
public class SetmealController {
	@Autowired
	private SetmealService setmealService;
	
	//分页列表查询
	@GetMapping("/setmeal/page")
	public ResultInfo findByPage(
			@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize,
			String name
	) {
		
		return ResultInfo.success(setmealService.findByPage(name, pageNum, pageSize));
	}
	
	@CacheEvict(value = "setmeal",allEntries = true)
	@PostMapping("/setmeal")
	public ResultInfo save(@RequestBody Setmeal setmeal) {
		setmealService.save(setmeal);
		return ResultInfo.success(null);
	}
	
	//删除
	@CacheEvict(value = "setmeal",allEntries = true)
	@DeleteMapping("/setmeal")
	public ResultInfo deleteByIds(Long[] ids) {
		if (ids != null && ids.length > 0) {
			setmealService.deleteByIds(ids);
		}
		return ResultInfo.success(null);
	}

	//获取套餐信息
	@GetMapping("/setmeal/{id}")
	public ResultInfo getSetmeal(@PathVariable("id") String id) {
		return ResultInfo.success(setmealService.findById(id));
		
	}
	
	//更改套餐信息
	@CacheEvict(value = "setmeal",allEntries = true)
	@PutMapping("/setmeal")
	public ResultInfo update(@RequestBody Setmeal setmeal) {
		setmealService.update(setmeal);
		return ResultInfo.success(null);
	}
	
	//批量起售停售
	@CacheEvict(value = "setmeal",allEntries = true)
	@PostMapping("/setmeal/status/{status}")
	public ResultInfo batchStopAndStart(
			@PathVariable("status") Integer status,
			@RequestParam("ids") List<String> ids) {
		setmealService.batchStopAndStart(status,ids);
		return ResultInfo.success(null);
	}
}
